package com.android.component;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class ChatInputActivity extends FragmentActivity implements View.OnClickListener {
    ListView listView;
    LayoutInflater layoutInflater;
    View btnSend;
    EditText etContent;
    View tv;

    List<String> msgs = new ArrayList<String>();
    MainAdapter adapter;
    View ex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        setContentView(R.layout.chat_input);
        listView = (ListView) findViewById(R.id.list);
        adapter = new MainAdapter();
        listView.setAdapter(adapter);
        btnSend = findViewById(R.id.send);
        etContent = (EditText) findViewById(R.id.EditTextMsgEdit);
        tv = findViewById(R.id.app_panel_layout);
        ex = findViewById(R.id.ivEmotion);
        ex.setOnClickListener(this);

        layoutInflater = getLayoutInflater();

        btnSend.setOnClickListener(this);
    }


    int count = 0;

    @Override
    public void onClick(View view) {
        if (view == btnSend) {
            msgs.add(etContent.getText().toString());
            adapter.notifyDataSetChanged();

        } else if (view == ex) {
            if (count == 0) {
                tv.setVisibility(View.GONE);
                goneSoftInput();
            } else {

                if (count == 1) {
                    tv.setVisibility(View.VISIBLE);
                    goneSoftInput();
                } else {
                    final InputMethodManager imm = (InputMethodManager) etContent.getContext().getSystemService(INPUT_METHOD_SERVICE);
//                    etContent.requestFocus();
                    imm.showSoftInput(etContent, 0);
                    tv.setVisibility(View.VISIBLE);
                }
            }
            count = ((count + 1) % 3);
        }
    }

    public void goneSoftInput() {
        if (this.getCurrentFocus() != null) {
            if (this.getCurrentFocus().getWindowToken() != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    class MainAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return msgs.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = layoutInflater.inflate(R.layout.chat_item, null);
            }
            TextView tv = (TextView) view.findViewById(R.id.tv);


            tv.setText(msgs.get(i));

            return view;

        }
    }


}
